package net.bingosoft.common.data;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.bingosoft.common.BeanHelper;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 2008-2-19 下午02:35:18
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:通用的值对象,是对Map 的简单封装,提供一些获取特定类型数据的方法
 * ChangeLog:
 */
public class ValueBean {
	private static final Log log = LogFactory.getLog(ValueBean.class);
	private Map <String,Object>container = new HashMap<String,Object>();
	/**
	 * 构造函数
	 * @param container
	 */
	public ValueBean(Map<String,Object> container){
		if(container != null && !container.isEmpty()){
			this.container.putAll(container);	
		}
	}
	/**
	 * 获取一个指定类型的对象
	 * @param columnName
	 * @return
	 */
    @SuppressWarnings("unchecked")
	public <T> T getTypedValue(String columnName,Class<T> type) {
    	Object value = container.get(columnName) == null ? 
    			(container.get(columnName.toUpperCase()) == null ? null : container.get(columnName.toUpperCase())) 
    			: container.get(columnName);
    	if(type.isInstance(value)){
    		return (T)value;
    	}else if(value == null){
    		return null;
    	}else if(type == String.class){
    		return (T)value.toString();
    	}else if(type == Date.class && value instanceof String){
    		return (T)BeanHelper.parseDate(value.toString());
    	}else{
    		try {
				Method m = type.getMethod("valueOf", String.class);
				if(m != null){
					T result = (T) m.invoke(type, value.toString());
					return result;	
				}
			} catch (Exception e) {
				log.debug("获取指定类型对象出错...",e);
			}
    		
    	}
    	return null;
    }   
    /**
     * 获取String类型的数据
     * @param columnName
     * @return
     */
    public String getString(String columnName){
    	String value = getTypedValue(columnName, String.class);
    	return value == null ? "" :value;
    }
    /**
     * 获取一个boolean类型的对象
     * @param columnName
     * @return
     */
    public boolean getBoolean(String columnName){
    	return getTypedValue(columnName, Boolean.class);
    }

    /**
     * 获取byte类型的对象
     * @param columnName
     * @return
     */
    public byte getByte(String columnName){
    	return getTypedValue(columnName, Byte.class);
    }

    /**
     * 获取short类型的对象
     * @param columnName 
     * @return
     */
    public short getShort(String columnName){
    	return getTypedValue(columnName, Short.class);
    }

    /**
     * 获取Integer类型的数据
     * @param columnName
     * @return
     */
    public int getInt(String columnName){
    	return getTypedValue(columnName, Integer.class);
    }

    /**
     * 获取Long类型的数据
     * @param columnName
     * @return
     */
    public long getLong(String columnName){
    	return getTypedValue(columnName, Long.class);
    }

    /**
     * 获取Float类型的数据
     * @param columnName
     * @return
     */
    public float getFloat(String columnName) {
    	return getTypedValue(columnName, Float.class);
    }

    /**
     * 获取double类型的数据
     * @param columnName
     * @return
     */
    public double getDouble(String columnName){
    	return getTypedValue(columnName, Double.class);
    }

    /**
     * 获取byte[]类型数据
     * @param columnName
     * @return
     */
    public byte[] getBytes(String columnName) {
    	return getString(columnName).getBytes();
    }

    /**
     * 获取日期类型数据
     * @param columnName
     * @return
     */
    public Date getDate(String columnName) {
    	return getTypedValue(columnName, Date.class);
    }
    
	@Override
	public String toString(){
		String selfDescription = null;
		try{
			selfDescription = ToStringBuilder.reflectionToString(this.container,ToStringStyle.MULTI_LINE_STYLE);
		}catch(Exception ex){
			selfDescription = super.toString();
		}
		return selfDescription;
	}
	
	/**
	 * 获取该ValueBean用于装载数据的Map
	 * @return
	 */
	public Map<String, Object> getValueMap() {
		return container;
	}


}
